package OrderService

import (
	"context"
	"gdshop-back-end-go/client-api/app/request/OrderReq"
	"gdshop-back-end-go/client-api/app/response/OrderResp"
	"gdshop-back-end-go/service-other/CouponService"
	"github.com/gogf/gf/util/guid"
)

// 获取预下单可使用的优惠券，提供给 doCreateByGroup 方法使用
func GetCreateAvailableUseCoupon(ctx context.Context, memberId int, goodss []*OrderResp.Goodss) ([]OrderReq.OrderSubCoupon, error) {
	// 取出我未使用的优惠券
	cList, err := CouponService.GetMyAvailable(ctx, memberId)
	if err != nil {
		return nil, err
	}
	var siList []*OrderReq.OrderSubItem
	for _, item := range goodss {
		siList = append(siList, &OrderReq.OrderSubItem{
			Details:    item.Details,
			Hash:       guid.S(),
			Total:      item.BuyNum,
			TotalPrice: item.Paid,
			OptionId:   int(item.Options[0].Id),
		})
	}
	// 判断符合当前商品的优惠券
	var useOrderSubs []OrderReq.OrderSubCoupon
	for _, item := range cList {
		tmps := calcCouponOne(siList, item)
		for _, t := range tmps {
			// 如果没有优惠金额，说明优惠券不能使用
			if t.Discount > 0 {
				useOrderSubs = append(useOrderSubs, t)
			}
		}
	}
	return useOrderSubs, nil
}
